package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名:");
        String username = scanner.nextLine();

        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        try(Connection conn = DBUtils.getConn();
        ){
            String sql = "select password from user where username=?";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,username);//1->第一个？

            //执行查询
            ResultSet resultSet = preparedStatement.executeQuery();

            //判断是否有数据
            if (resultSet.next()) {
                String pw = resultSet.getString(1);//1->第一个数据
                if (pw.equals(password)) {
                    System.out.println("登录成功");
                }
                else {
                    System.out.println("密码错误");
                }
            }
            else {
                System.out.println("用户名不存在");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
